package com.twu.biblioteca.model;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by davecroman on 19/09/2014.
 */
public class Movie extends BorrowableAsset implements Comparable<Movie>{

    private static AtomicInteger idGenerator = new AtomicInteger();

    private String name;
    private int year;
    private String director;
    private int rating; /* 1 is lowest and 10 is highest, -1 is unrated */
    private Integer id;

    /* Use MovieBuilder class to instantiate */
    private Movie(String name, int year, String director, int rating) {
        this.name = name;
        this.year = year;
        this.director = director;
        this.rating = rating;
        this.id = idGenerator.incrementAndGet();

    }

    public String getName() {
        return name;
    }

    public int getYear() {
        return year;
    }

    public String getDirector() {
        return director;
    }

    public int getRating() {
        return rating;
    }

    public Integer getId(){
        return id;
    }

    @Override
    public String toString() {
        return String.format("[%d] %-25s | %-25s | %-4d | %-7s", id, name, director, year, rating!=-1? rating : "Unrated");
    }

    @Override
    public int compareTo(Movie movie) {
        return name.compareTo(movie.getName());
    }

    /**
     * Created by davecroman on 19/09/2014.
     */
    public static class MovieBuilder {

        private int rating;
        private String name;
        private int date;
        private String director;

        public MovieBuilder(){
            this.rating = -1;  // -1 is unrated
        }

        public MovieBuilder setName(String name){
            this.name = name;
            return this;
        }

        public MovieBuilder setYear(int date){
            if( date >= 1870 ) {
                this.date = date;
            }else{
                throw new IllegalArgumentException("Date must be greater than 1869");
            }
            return this;
        }

        public MovieBuilder setDirector(String director){
            this.director = director;
            return this;
        }

        /**
         * Rating should be between 1 and 10, inclusive
         */
        public MovieBuilder setRating(int rating){
            if( rating >=1 && rating <=10 ){
                this.rating = rating;
            }else{
                throw new IllegalArgumentException("Rating should be between 1-10, inclusive");
            }
            return this;
        }

        /**
         * Instantiates a new Movie object. This fails when 1 or more
         * of the following conditions are true: (1) name or director is null,
         * (2) date is not specified
         */
        public Movie build(){
            if( name != null && date != 0 && director != null ){
                return new Movie(name,date,director,rating);
            }else{
                throw new IllegalArgumentException("One or more required parameters not specified");
            }
        }
    }
}
